﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print_UI
{
    public partial class FrmCheckSetorID : CommonForm
    {
        public FrmCheckSetorID()
        {
            InitializeComponent();
        }
        public DataTable tbClassInSector;
        public int iDepartment;
        private void FrmCheckSetorID_Load(object sender, EventArgs e)
        {

            grdSectorID.DataSource = tbClassInSector;
            LoadSector();
        }
        private void LoadSector()
        {

            try
            {

                var sector = from obj in Model.CAT_Sector.Where(c => c.DepartmentID == iDepartment)
                             select new { SectorID = obj.ID, obj.Name };
                repositoryItemLookUpEdit1.DataSource = sector;
                repositoryItemLookUpEdit1.DisplayMember = "Name";
                repositoryItemLookUpEdit1.ValueMember = "SectorID";

            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }

        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {

                int count = 0;

                for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                {

                    DataRow dr = tbClassInSector.Rows[irow];
                    int iClassID = Convert.ToInt32(dr["ID"]);
                    int iSector = Convert.ToInt32(dr["SectorID"]);
                    if (iSector == -1)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    if (MessageBox.Show("Lưu Thay Đổi?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {

                        for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                        {

                            DataRow dr = tbClassInSector.Rows[irow];
                            int iClassID = Convert.ToInt32(dr["ID"]);
                            int iSector = Convert.ToInt32(dr["SectorID"]);

                            var obj = Model.CAT_Class.Where(c => c.ID == iClassID).FirstOrDefault();
                            if (obj != null)
                                obj.SectorID = iSector;
                        }
                        Model.SaveChanges();
                    }
                    for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                    {

                        int iClassID = Convert.ToInt32(tbClassInSector.Rows[irow]["ID"]);
                        var obj = Model.CAT_Class.Where(c => c.ID == iClassID).FirstOrDefault();
                        if (obj != null)
                            tbClassInSector.Rows[irow]["SectorID"] = obj.SectorID;
                    }
                    this.Close();
                }
                else if (count > 0)
                {
                    MessageBox.Show("Chưa chọn nghành cho lớp, Vui Lòng kiểm tra lại", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
              try
            {

                int count = 0;

                for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                {

                    DataRow dr = tbClassInSector.Rows[irow];
                    int iClassID = Convert.ToInt32(dr["ID"]);
                    int iSector = Convert.ToInt32(dr["SectorID"]);
                    if (iSector == -1)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    if (MessageBox.Show("Lưu Thay Đổi?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {

                        for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                        {

                            DataRow dr = tbClassInSector.Rows[irow];
                            int iClassID = Convert.ToInt32(dr["ID"]);
                            int iSector = Convert.ToInt32(dr["SectorID"]);

                            var obj = Model.CAT_Class.Where(c => c.ID == iClassID).FirstOrDefault();
                            if (obj != null)
                                obj.SectorID = iSector;
                        }
                        Model.SaveChanges();
                    }
                    for (int irow = 0; irow < tbClassInSector.Rows.Count; irow++)
                    {

                        int iClassID = Convert.ToInt32(tbClassInSector.Rows[irow]["ID"]);
                        var obj = Model.CAT_Class.Where(c => c.ID == iClassID).FirstOrDefault();
                        if (obj != null)
                            tbClassInSector.Rows[irow]["SectorID"] = obj.SectorID;
                    }
                    this.Close();
                }
                else if (count > 0)
                {
                    MessageBox.Show("Chưa chọn nghành cho lớp, Vui Lòng kiểm tra lại", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        }

      
    
}
